package com.csu.ourpetstore1.controller;

import com.csu.ourpetstore1.domain.CartItem;
import com.csu.ourpetstore1.domain.Item;
import com.csu.ourpetstore1.service.CartService;
import com.csu.ourpetstore1.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/cart")
@SessionAttributes("username")
public class CartController {

    @Autowired
    CartService cartService;

    @Autowired
    CatalogService catalogService;
    //查看购物车
    @GetMapping("/viewItem")
    public String ViewCart(String username, Model model){
        List<CartItem> cart = cartService.getCart(username);
        model.addAttribute("cart",cart);
        return "cart/cart";
    }
    //添加商品进购物车
    @GetMapping("/addItemToCart")
    public String AddItemToCart(String username, String workingItemId, Model model){
        if(workingItemId!=null){
            int isInStock = catalogService.isItemInStock(workingItemId);
            Item item = catalogService.getItem(workingItemId);
            cartService.addItem(username,item, isInStock);

            List<CartItem> Cart = cartService.getCart(username);
            model.addAttribute("cart",Cart);
            return "cart/cart";
        }
        else return "common/error";
    }
    //从购物车中移除商品
    @GetMapping("/removeItemFromCart")
    public String RemoveItemFromCart(String username,String workingItemId, Model model){
        if(workingItemId!=null){
            Item item = cartService.remove(username,workingItemId);

            List<CartItem> Cart = cartService.getCart(username);
            model.addAttribute("cart",Cart);
            return "cart/cart";
        }
        else return "common/error";
    }
    //更新购物车
    @PostMapping("/updateCartQuantities")
    public String UpdateCartQuantities(@SessionAttribute String username,Model model){
        List<CartItem> Cart = cartService.getCart(username);
        if(Cart!=null){
            for (CartItem cartItem : Cart) {
                String itemId = cartItem.getItem().getItemId();
                try {
                    HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
                    int quantity1 = Integer.parseInt(request.getParameter(itemId));
                    cartService.setQuantity(username,itemId, quantity1);
                    if (quantity1 < 1) {
                        cartService.remove(username, itemId);
                    }
                } catch (Exception e) {
                    model.addAttribute("message", "The Quantities of Item must be Integer!");
                    return "common/error";
                }
            }
        }
        Cart = cartService.getCart(username);
        model.addAttribute("cart",Cart);
        return "cart/cart";
    }
}
